package com.xxxx.server.config.security.rabbitmq;
import com.rabbitmq.client.Channel;
import com.xxxx.server.pojo.Employee;
import com.xxxx.server.pojo.MailConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.mail.MailProperties;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMailMessage;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;
import org.springframework.stereotype.Component;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.IOException;
import java.time.LocalDate;
import java.util.Date;
@Component
public class MailReceiver {
    private static final Logger LOGGER = LoggerFactory.getLogger(MailReceiver.class);
    @Autowired
    private MailProperties mailProperties;
    @Autowired
    private JavaMailSender javaMailSender;
    @Autowired
    private TemplateEngine templateEngine;
    @Autowired
    private RedisTemplate redisTemplate;

    @RabbitListener(queues = MailConstants.MAIL_QUEUE_NAME)
    public void handler(Message message, Channel channel) throws IOException {
        MessageHeaders headers =message.getHeaders();
        long tag=(long)headers.get(AmqpHeaders.DELIVERY_TAG);
        Employee employee=(Employee) message.getPayload();
        String msgId=(String) headers.get("spring_returned_message_correlation");
        HashOperations hashOperations =redisTemplate.opsForHash();

        try {

            if (hashOperations.entries("mail_log").containsKey(msgId)){
                LOGGER.error("消息已经被确认====>{}",msgId);
                channel.basicAck(tag,false);
                return;
            }

            MimeMessage msg2=javaMailSender.createMimeMessage();
            MimeMessageHelper mimeMessageHelper2=new MimeMessageHelper(msg2);
            mimeMessageHelper2.setFrom(mailProperties.getUsername());
            mimeMessageHelper2.setTo(employee.getEmail());
            mimeMessageHelper2.setSubject("欢迎入职");
            mimeMessageHelper2.setSentDate(new Date());

            Context context =new Context();
            context.setVariable("name",employee.getName());
            context.setVariable("posName",employee.getPosition().getName());
            context.setVariable("joblevelName",employee.getJoblevel().getName());
            context.setVariable("departmentName",employee.getDepartment().getName());

            String mail=templateEngine.process("mail",context);
            mimeMessageHelper2.setText(mail,true);
            javaMailSender.send(msg2);

            LOGGER.info("邮件发送成功====>");
            hashOperations.put("mail_log",msgId,"OK");
            channel.basicAck(tag,false);
        } catch (MessagingException | IOException e) {
            channel.basicNack(tag,false,true);
            e.printStackTrace();
        }



    }
}
